﻿using System;
using System.Linq;
using ECommerce.Controls;
using Service;
using Data.Entity;

namespace ECommerce.Customer.Controls
{
	public partial class CountryProvinceCitySelector : BaseUserControl
	{
		private ICountryService _CountryService;
		private IStateProvinceService _StateProvinceService;

		public int SelectedCountryID
		{
			get
			{
				int id = 0;

				int.TryParse(DropDownListCountry.SelectedValue, out id);

				return id;
			}
			set
			{
				int v = value;
				if(v > 0)
				{
					if(DropDownListCountry.Items == null || DropDownListCountry.Items.Count <= 0)
					{
						BindCountry();
					}

					if(DropDownListCountry != null && DropDownListCountry.Items.Count > 0)
					{
						DropDownListCountry.SelectedValue = v.ToString();
						DropDownListCountry_SelectedIndexChanged(DropDownListCountry, null);
					}
				}
			}
		}

		public int SelectedProvinceID
		{
			get
			{
				int id = 0;

				int.TryParse(DropDownListProvince.SelectedValue, out id);

				return id;
			}
			set
			{
				int v = value;
				if(v > 0)
				{
					if(DropDownListProvince.Items == null || DropDownListProvince.Items.Count <= 0)
					{
						BindStateProviceByCountry();
					}

					if(DropDownListProvince.Items != null && DropDownListProvince.Items.Count > 0)
					{
						DropDownListProvince.SelectedValue = v.ToString();
						DropDownListProvince_SelectedIndexChanged(DropDownListProvince, null);
					}
				}
			}
		}

		public int SelectedCityID
		{
			get
			{
				int id = 0;

				int.TryParse(DropDownListCity.SelectedValue, out id);

				return id;
			}
			set
			{
				int v = value;
				if(v > 0)
				{
					if(DropDownListCity.Items == null && DropDownListCity.Items.Count <= 0)
					{
						BindCityByStateProvince();
					}

					if(DropDownListCity.Items != null && DropDownListCity.Items.Count > 0)
					{
						DropDownListCity.SelectedValue = v.ToString();
					}
				}
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				BindCountry();
			}
		}

		protected void DropDownListProvince_SelectedIndexChanged(object sender, EventArgs e)
		{
			BindCityByStateProvince();
		}

		protected void DropDownListCountry_SelectedIndexChanged(object sender, EventArgs e)
		{
			BindStateProviceByCountry();
		}

		private void BindCountry()
		{
			if(_CountryService == null)
			{
				_CountryService = new CountryService();
			}

			var countries = _CountryService.GetAllCountry();

			if (countries != null && countries.Count > 0)
			{
				countries.Insert(0, new Country(0, string.Empty, false, false, false, string.Empty, string.Empty, 0, false, 0));

				this.DropDownListCountry.DataSource = countries;
				this.DropDownListCountry.DataBind();
			}
		}

		private void BindStateProviceByCountry()
		{
			int selectedCountryID = 0;

			if(int.TryParse(DropDownListCountry.SelectedValue, out selectedCountryID))
			{
				if(selectedCountryID > 0)
				{
					if(_StateProvinceService == null)
					{
						_StateProvinceService = new StateProvinceService();
					}

					var sps = _StateProvinceService.GetStateProvinceByCountry(selectedCountryID);

					if(sps != null && sps.Count > 0)
					{
						sps.Insert(0, new StateProvince(0, 0, string.Empty, string.Empty, 0));

						this.DropDownListProvince.DataSource = sps;
						this.DropDownListProvince.DataBind();
					}
				}
				else
				{
					this.DropDownListProvince.Items.Clear();
					this.DropDownListCity.Items.Clear();
				}
			}
		}

		private void BindCityByStateProvince()
		{
			int selectedStateProvinceID = 0;

			if(int.TryParse(DropDownListProvince.SelectedValue, out selectedStateProvinceID))
			{
				if(selectedStateProvinceID > 0)
				{
					if(_StateProvinceService == null)
					{
						_StateProvinceService = new StateProvinceService();
					}

					var cities = _StateProvinceService.GetCityByStateProvince(selectedStateProvinceID);

					if(cities != null && cities.Count > 0)
					{
						var c0 = new City();
						c0.CityID = 0;
						c0.CityDescription = string.Empty;
						c0.CityName = string.Empty;
						c0.DisplayOrder = 0;
						c0.StateProvinceID = 0;
						cities.Insert(0, c0);
					}

					this.DropDownListCity.DataSource = cities;
					this.DropDownListCity.DataBind();
				}
				else
				{
					this.DropDownListCity.Items.Clear();
				}
			}
		}
	}
}